(function() {
    'use strict';
    function count(o){
        var t = typeof o;
        if(t == 'string'){
            return o.length;
        }else if(t == 'object'){
            var n = 0;
            for(var i in o){
                n++;
            }
            return n;
        }
        return false;
    };
    angular
        .module('app.studentlist',['getdatamodule','toastr','tm.pagination','ui.bootstrap'])
        .filter('unique', function() {
        return function (arr, field) {

            var o = {}, i, l = count(arr), r = [];
            for(i=0; i<l;i+=1) {
                o[arr[i][field]] = arr[i];
            }
            for(i in o) {
                r.push(o[i]);
            }
            return r;
        };
    })

        .controller("StudentListController", StudentListController)

    ;
    StudentListController.$inject = [ '$state', '$scope','getdataservice','$uibModal'];

    function StudentListController( $state, $scope,getdataservice, $uibModal) {

        var studentfiler="";
        var students=[];
        var filerdata=[];
        getdataservice.all("/student/getshengfen/?id="+new Date().getTime()).then(function (data) {
            $scope.studentshengfen=data.Data;
            $scope.isLoading=false;
        },function (data) {
            $scope.isLoading=false;
        });
        getdataservice.all("/special/index/?id="+new Date().getTime()).then(function (data) {
            $scope.zhuanyelist=data.Data;
            $scope.Spe_ID=$scope.zhuanyelist[0].Spe_id;
            $scope.isLoading=false;
        },function (data) {
            $scope.isLoading=false;
        });
        $scope.url=baseurl+"/Excel/GetStudentsExcel/";
        $scope.Student_id="";
        $scope.Student_name="";
        $scope.Student_shengfen="";
        $scope.Student_isbd="";
        $scope.Student_tzsh="";
        $scope.paginationConf = {
            currentPage: 1,
            totalItems: 0,
            itemsPerPage: 10,
            pagesLength: 5,
            perPageOptions: [10, 20, 30, 40, 50],
            onChange: function(){
                $scope.studentdisplay=[].concat(filerdata).slice(($scope.paginationConf.currentPage-1) * $scope.paginationConf.itemsPerPage,$scope.paginationConf.currentPage * $scope.paginationConf.itemsPerPage);
            }
        };
            $scope.resetall=function () {
                $scope.Student_id="";
                $scope.Student_name="";
                $scope.Student_shengfen="";
                $scope.Student_isbd="";
                $scope.Student_tzsh="";
                $scope.Spe_ID=$scope.zhuanyelist[0].Spe_id;
            }
            $scope.callserver=function  (tablestate) {
                //console.log(tablestate);
                getdataservice.getpate(0, $scope.paginationConf.itemsPerPage,tablestate,students).then(function (result) {

                    $scope.totalnums=result.totalItems;
                    $scope.page=result.numberOfPages;
                    filerdata=result.filtereddata;
                    $scope.paginationConf.totalItems=filerdata.length;
                    $scope.studentdisplay=[].concat(filerdata).slice(($scope.paginationConf.currentPage-1) * $scope.paginationConf.itemsPerPage,$scope.paginationConf.currentPage * $scope.paginationConf.itemsPerPage);
                    //pagination.totalItemCount = result.totalItems;
                    //pagination.maxSize = pagination.totalItemCount/pagination.number;
                })
            };
        $scope.getStudent = function () {
            console.log($scope.Student_id);
            console.log($scope.Student_name);
            console.log($scope.Spe_ID);
            console.log($scope.Student_tzsh);
            console.log($scope.Student_shengfen);
            console.log($scope.Student_isbd);
            $scope.isLoading=true;
            studentfiler="&tid="+$scope.Student_id+"&tname="+$scope.Student_name+"&speid="+$scope.Spe_ID+"&ttzsh="+$scope.Student_tzsh+"&tshengfen="+$scope.Student_shengfen+"&tisbd="+$scope.Student_isbd;
            getdataservice.all("/Student/GetStudents/?id="+new Date().getTime()+"&p="+$scope.paginationConf.currentPage+"&psize="+$scope.paginationConf.itemsPerPage+studentfiler).then(function (data) {
                $scope.studentlist=data.Data;
                students=$scope.studentlist;
                $scope.isLoading=false;
                $scope.studentdisplay=[].concat(students).slice(0,$scope.paginationConf.itemsPerPage);
                filerdata=students;
                $scope.paginationConf.totalItems=filerdata.length;
                return $scope.studentlist;
            },function (data) {
                $scope.isLoading=false;
            });
        };

        $scope.ShowDetail=function (stuid) {
            getdataservice.all("/Student/Details/"+stuid+"/?id="+new Date().getTime()).then(
                function (data) {
                    if (data.Data.length<=0)
                    {
                        swal("参数错误，没有该生数据","错误！","error");
                        return;
                    }

                    $uibModal.open({
                        animation:true,
                        backdrop:true,
                        keyboard:false,
                        appendTo:angular.element('tbody'),
                        templateUrl: 'app/views/student/detail.html',
                        controller: 'StudentDetailCtrl',
                        size:'md',
                        resolve:{
                            studentdetail:function () {
                                return data.Data;
                            },

                        },

                    });

                },function (data) {

                }
            )

        }

    }
})();
